package com.gl.saasservice.sreach.service;

import com.alibaba.fastjson.JSONObject;
import com.gl.common.vo.ApiData;
import com.gl.framework.web.response.Result;
import com.gl.saasservice.sreach.entity.AppKeywordSreach;
import com.gl.saasservice.sreach.repository.AppKeywordSreachRepository;
import com.gl.saasservice.sreach.vo.AppKeywordSreachVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class AppKeywordSreachService {
    private static final Logger log = LoggerFactory.getLogger(AppKeywordSreachService.class);

    @Autowired
    private AppKeywordSreachRepository appKeywordSreachRepository;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public Result pages(String accessToken, Integer pageNumber, Integer pageSize, AppKeywordSreachVO vo){
        log.info("AppKeywordSreachService  pages  accessToken={},vo={} ",accessToken, JSONObject.toJSONString(vo));

        String SQL=" SELECT ks.* FROM saas_app_keyword_sreach ks  ";

        String count=" SELECT count(1) FROM saas_app_keyword_sreach ks  ";

        String offsets = " limit ? offset ?";

        StringBuilder where = new StringBuilder("WHERE 1 = 1  ");
        List<Object> args = new ArrayList<>();

        if(vo != null){
            if (StringUtils.isNotBlank(vo.getKeyword())){
                where.append(" AND ks.keyword like ? \n");
                args.add("%"+vo.getKeyword()+"%");
            }

        }

        ApiData<AppKeywordSreachVO> apiData = new ApiData<>();

        Long total = jdbcTemplate.queryForObject(count + where, Long.class, args.toArray());
        if (total == 0) {
            return Result.success(apiData);
        }

        // 查询分页数据
        where.append(" ORDER BY ").append(" ks.create_time desc ").append(" \n").append(offsets);
        args.add(pageSize);
        args.add(pageNumber * pageSize);
        List<AppKeywordSreachVO> data = jdbcTemplate.query(SQL + where, new BeanPropertyRowMapper<>(AppKeywordSreachVO.class), args.toArray());

        apiData.setTotal(total);
        apiData.setData(data);
        return Result.success(apiData);
    }

    public Result save(String accessToken ,AppKeywordSreachVO vo){
        log.info("AppKeywordSreachService save  accessToken={},vo={}",accessToken,vo.toString() );

        if(StringUtils.isBlank(vo.getKeyword())){
            return Result.fail("搜索关键字不能为空");
        }
        if(StringUtils.isBlank(vo.getPath())){
            return Result.fail("APP路径不能为空");
        }

        AppKeywordSreach appKeywordSreach =new AppKeywordSreach();

        BeanUtils.copyProperties(vo,appKeywordSreach);

        if(appKeywordSreach.getId() ==null){
            appKeywordSreach.setCreateTime(new Date());
        }

        appKeywordSreachRepository.save(appKeywordSreach);

        return Result.success(null);
    }

    public Result delete(String accessToken,Long id){
        log.info("AppKeywordSreachService delete  accessToken={},id={}",accessToken,id );

        Optional<AppKeywordSreach> optional= appKeywordSreachRepository.findById(id);
        if(!optional.isPresent()){
            return Result.fail("当前数据不存在！");
        }

        appKeywordSreachRepository.deleteById(id);

        return Result.success(null);
    }


}
